/**
 * @author gao
 * @date 2025/11/5 16:20
 */

class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public static ListNode unionList(ListNode A, ListNode B) {
        ListNode C = new ListNode(0);
        ListNode curC = C;

        while (A != null && B != null) {
            if (A.val <= B.val) {
                curC.next = A;
                A = A.next;
            } else {
                curC.next = B;
                B = B.next;
            }
            curC = curC.next;
        }

        if (A != null) {
            curC.next = A;
        }

        if (B != null) {
            curC.next = B;
        }

        return C.next;
    }

    public static void main(String[] args) {
        int[] arrayA = new int[]{3, 5, 8, 11};
        int[] arrayB = new int[]{2, 6, 8, 9, 11, 15, 20};

        ListNode LA = createList(arrayA);
        ListNode LB = createList(arrayB);

        ListNode LC = unionList(LA, LB);

        System.out.println("===");
    }

    private static ListNode createList(int[] array) {
        if (array == null || array.length == 0) {
            return null;
        }

        ListNode head = new ListNode(array[0]);
        ListNode cur = head;

        for (int i = 1; i < array.length; i++) {
            ListNode tail = new ListNode(array[i]);
            cur.next = tail;
            cur = tail;
        }
        return head;
    }
}